package com.platform.wechat.service.impl;

import com.platform.wechat.common.JsonBean;
import com.platform.wechat.entity.Patient;
import com.platform.wechat.entity.Platform;
import com.platform.wechat.mapper.PatientMapper;
import com.platform.wechat.mapper.PlatformMapper;
import com.platform.wechat.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 就诊人
 */
@Service
public class PatientServiceImpl implements PatientService {

    /**
     * 调用mapper
     */
    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private PlatformMapper platformMapper;
    /**
     * 添加就诊人
     * @param patient
     * @return
     */
    @Override
    public JsonBean insertPatient(Patient patient,String phone) {
        JsonBean jsonBean = new JsonBean(-1, "失败", null);
        Platform platform = platformMapper.selectPrismaryBys(phone);
        //查询人数
        int a = patientMapper.selectperson(platform.getPlatformId());
        if (a>5){
            jsonBean.setMsg("人数超过四人");
        }else if(a==4){
            jsonBean.setMsg("人数已满");
        }else{
            //添加
            patient.setPlatformaccountId(platform.getPlatformId());
            int i = patientMapper.insertSelective(patient);
            if (i==1){
                jsonBean.setCode(0);
                jsonBean.setMsg("ok");
            }
        }
        return jsonBean;
    }

    /**
     * 删除就诊人
     * @param patientId
     * @return
     */
    @Override
    public JsonBean deletePatient(Integer patientId) {
        JsonBean jsonBean = new JsonBean(-1, "失败", null);
        if (patientId!=null){
            int i = patientMapper.deleteByPrimaryKey(patientId);
            if (i==1){
                jsonBean.setCode(0);
                jsonBean.setMsg("ok");
            }
        }
        return jsonBean;
    }

    /**
     * 修改就诊人信息
     * @param patient
     * @return
     */
    @Override
    public JsonBean updatePatient(Patient patient) {
        JsonBean jsonBean = new JsonBean(-1, "失败", null);
            int i = patientMapper.updateByPrimaryKeySelective(patient);
            if (i==1){
                jsonBean.setCode(0);
                jsonBean.setMsg("ok");
            }
        return jsonBean;
    }

    /**
     * 查询就诊人详细信息
     * @param patientId
     * @return
     */
    @Override
    public JsonBean selectBy(Integer patientId) {
        Patient patient=null;
        if (patientId!=null){
            patient = patientMapper.selectByPrimaryKey(patientId);
            System.err.println(patient);
        }
        return new JsonBean(0,"ok",patient);
    }

    /**
     * 查询所有就诊人
     * @return
     */
    @Override
    public JsonBean selectPatient(String phone) {
        Platform platform = platformMapper.selectPrismaryBys(phone);
        List<Patient> patients = patientMapper.selectPrimary(platform.getPlatformId());
        return new JsonBean(0,"ok",patients);
    }

    @Override
    public JsonBean selectByCardId(String cardId) {
        return new JsonBean(0, "ok", patientMapper.selectPatientByCardId(cardId));
    }
}
